<?php

namespace app\models;

use yii;
use app\models\Manager;
use app\models\FunctionPointExt;

class Permission
{
	const SUPER_ADMIN_ID = 0; // 超级管理员id
	const TEST = true; // todo 功能点数据完成后请删除这部分

	private static $permission_list = []; // 当前manager的权限列表

	public static function getPermissonList()
	{
		if (empty(self::$permission_list)) {
			$manager = Manager::getCurrentManager();

			if ($manager && isset($manager['id'])) {
				self::$permission_list = FunctionPointExt::getFunctionPointListByManagerId($manager['id']);
			}
		}
		return self::$permission_list;
	}

	/**
	 * 判断当前用户对该controller和控制器是否有权限
	 * @param  string $controller
	 * @param  string $action
	 * @return boolean
	 * @author milloc
	 * @date   2018-04-27
	 */
	public static function hasPermission($controller, $action)
	{
		// todo 功能点数据完成后请删除这部分
		if (self::TEST) {
			return true;
		}

		$manager = Manager::getCurrentManager();
		if ($manager && $manager['id'] === self::SUPER_ADMIN_ID) {
			return true;
		}

		if (!$controller || !$action) {
			return false;
		}

		$permission_list = self::getPermissonList();
		return empty($permission_list) ? false : in_array([$controller, $action], $permission_list);
	}
}